<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class MessageService extends Service{

    public static function indexHeader(){
        return array(
				'id'             => array('text' => 'ID', 'rule' => 'int'),
        		'channel_id'     => array('text' => '渠道'),
				'server_id'      => array('text' => '服务器'),
				'role_id'        => array('text' => '角色ID'),
				'role_name'      => array('text' => '角色名称'),
        		'role_level'     => array('text' => '角色等级'),
				'desc'           => array('text' => '反馈内容','width' => '350px'),
				'create_time'    => array('text' => '反馈时间'),
        		'is_reply'       => array('text' => '已经回复'),
				'action'         => array('text' => '操作')
		);

        return $header;
    }
    
    public static function indexData($params){
    	$modMessage = new ModMessage();
    	$result = $modMessage->page($params);
    	
    	$condition = array();
    	$condition['g'] = $params['g'];
    	$modChannel = new ModChannel();
    	$channel_array = $modChannel->select($condition);
    	$channel_list = array();
    	foreach ($channel_array as $key=>$val){
    		$channel_list[$val['id']] = $val['channel_name'];
    	}
    	
    	$serverList = PubServer::getOptions($platform, $game, 'id');
    	$isReplyList = self::isReplyList();
    	$length = count($result['data']);
    	foreach($result['data'] as &$v){
    		$type = 1;
    		if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
    			$type = 2;
    		}
    		$v['channel_id'] = $channel_list[$v['channel_id']];
    		$v['server_id'] = $serverList[$v['server_id']];
    		$v['desc'] = '<div style="white-space:normal;">'.$v['desc'].'</div>';
    		$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
    		$v['is_reply'] = $isReplyList[$v['is_reply']];
    		//$v['action']  .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=message/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
    		$v['action']        .= '&nbsp;&nbsp;<a href="javascript:;" name="reply" data-id="'.$v['id'].'">回复</a>';
    		$v['action']        .= '&nbsp;&nbsp;<a href="javascript:;" name="reply_view" data-id="'.$v['id'].'">查看回复</a>';
    	}
    	unset($v);
    	return $result;
    }


    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modReplyTemplate = new ModReplyTemplate();
    	$modReplyTemplate->deleteByPk($id);
    	return true;
    }
    
    public static function isReplyList(){
    	$isReplyList = array(
    			'1'=>'否',
    			'2'=>'<font color="#428bca">'.'是'.'</font>'
    	);
    	return $isReplyList;
    }
    
    /**
     * 申请邮件回复
     * @param unknown $ar
     * @return multitype:number string
     */
    public static function reply($ar){
    	if($ar['message_id'] == ''){
    		$out = array('status'=>0,'msg'=>'非法参数','data'=>'');
    		return $out;
    	}
    	
    	$message_id = $ar['message_id'];
    	//查询反馈信息
    	$modMessage = new ModMessage();
        $message_obj = $modMessage->findByPk($message_id);
        if(!$message_obj){
        	$out = array('status'=>0,'msg'=>'找不到该信息','data'=>'');
        	return $out;
        }
        $message_info = $message_obj->getAttributes();
        $server_id = $message_info['server_id'];

        $modServer = new ModServer();
        $server_obj = $modServer->findByPk($server_id);
        if(!$server_obj){
        	$out = array('status'=>0,'msg'=>'找不到服务器','data'=>'');
        	return $out;
        }
        $server_info = $server_obj->getAttributes();
        $server_name = $server_info['server_name'];
    	
        $modSend = new ModSend();
        $game = PubBase::getCurrentGame();
        $user = AccAuth::me();
    	if($ar['is_use_templet'] == 1){ //使用模板
    		if($ar['templet'] == ''){
    			$out = array('status'=>0,'msg'=>'请选择回复模板','data'=>'');
    			return $out;
    		}
    		
    		//查询模板信息
    		$modReplyTemplate = new ModReplyTemplate();
    		$template_obj = $modReplyTemplate->findByPk($ar['templet']);
    		if(!$template_obj){
    			$out = array('status'=>0,'msg'=>'找不到该回复模板','data'=>'');
    			return $out;
    		}
    		$template_info = $template_obj->getAttributes();
    		
    		$modSend->platform = 'test';
    		$modSend->game = $game;
    		$modSend->server_id  = $server_id;
    		$modSend->server_name  = $server_name;
    		$modSend->send_type = 1;
    		$modSend->receive = $message_info['role_name'];
    		$modSend->title = $template_info['title'];
    		$modSend->content = $template_info['content'];
    		$modSend->item = $template_info['item'];
    		$modSend->create_user   = $user['UserID'];
    		$modSend->create_time   = time();
    		$modSend->update_user   = $user['UserID'];
    		$modSend->update_time   = time();
    		$modSend->status = 1;
    		$result = $modSend->save();
    		
    		
    	}else{ //不使用模板
    		$item = '';
    		if($ar['is_send_good'] == 2){ //不发送道具
    			$item = '';
    		}
    			
    		if($ar['is_send_good'] == 1){ //发送道具
    			$item_id_array = explode(',', $ar['item_id']);
    			$item_name_array = explode(',', $ar['item_name']);
    			$item_number_array = explode(',', $ar['item_number']);
    			$is_bind_array = explode(',', $ar['is_bind']);
    			
    			$person_item = array();
    			foreach ($item_id_array as $key=>$val){
    				$data = array();
    				$data['item_name'] = $item_name_array[$key];
    				$data['item_id'] = $item_id_array[$key];
    				$data['item_number'] = $item_number_array[$key];
    				$data['is_bind'] = $is_bind_array[$key];
    				array_push($person_item, $data);
    			}
    			$item = json_encode($person_item);
    		}
    		
    		$modSend->platform = 'test';
    		$modSend->game = $game;
    		$modSend->server_id  = $server_id;
    		$modSend->server_name  = $server_name;
    		$modSend->send_type = 1;
    		$modSend->receive = $message_info['role_name'];
    		$modSend->title = $ar['title'];
    		$modSend->content = $ar['content'];
    		$modSend->item = $item;
    		$modSend->create_user   = $user['UserID'];
    		$modSend->create_time   = time();
    		$modSend->update_user   = $user['UserID'];
    		$modSend->update_time   = time();
    		$modSend->status = 1;
    		$result = $modSend->save();
    		
    	}
    	
    	if(!$result){
    		$out = array('status'=>0,'msg'=>'添加回复申请邮件失败','data'=>'');
    		return $out;
    	}
    	    	
    	//增加反馈和邮件关系的记录
    	$modMessageReply = new ModMessageReply();
    	$send_person_id = $modSend->id;
    	$modMessageReply->send_person_id = $send_person_id;
    	$modMessageReply->message_id = $message_id;
    	$modMessageReply->create_user   = $user['UserID'];
    	$modMessageReply->create_time   = time();
    	$modMessageReply->update_user   = $user['UserID'];
    	$modMessageReply->update_time   = time();
    	$mr_result = $modMessageReply->save();
    	
    	$update = array();
    	$update['is_reply'] = 2;
    	$update['update_user']  = $user['UserID'];
    	$update['update_time']  = time();
    	$m_result = $modMessage->updateByPk($message_id, $update);
    	
    	$out = array('status'=>1,'msg'=>'回复邮件申请成功','data'=>'');
    	return $out;
    }
    
    /**
     * 查看回复
     * @param unknown $message_id
     */
    public static function reply_view($message_id){
    	$modMessage = new ModMessage();
    	$data = $modMessage->replyView($message_id);
    	$list = array();
    	$userList = self::userList();
    	$statusList = SendService::status();

    	foreach ($data as $key=>$val){
    		$item_array = json_decode($val['item'],true);
    		$item = '';
    		foreach ($item_array as $k=>$v){
    			if($v['is_bind'] == 1){
    				$bind = '绑定';
    			}
    			if($v['is_bind'] == 0){
    				$bind = '不绑定';
    			}
    			$item .= $v['item_name'].'-'.$v['item_id'].'-'.$v['item_number'].'-'.$bind;
    			$item .= '</br>';
    		}
    		
    		$d = array();
    		$d['create_user'] = $userList[$val['create_user']];
    		$d['create_time'] = date('Y-m-d H:i:s', $val['create_time']);
    		$d['title'] = $val['title'];
    		$d['content'] = $val['content'];
    		$d['item'] = $item;
    		$d['status'] = $statusList[$val['status']];
    		array_push($list, $d);
    	}
    	
    	return $list;
    }


}